package com.solr;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.*;

/**
 * @program: Solr
 * @description:
 * @author: Dawson.Zhang
 * @create: 2020-03-10
 **/
public class ASolrTest {
    private final static String BASE_URL = "http://192.168.117.128:8080/solr/collection1";

    public static HttpSolrClient getSolrClient() {
        return new HttpSolrClient.Builder(BASE_URL).build();
    }

//		 <field column="pid" name="id"/>
//		 <field column="name" name="product_name"/>
//		 <field column="catalog_name" name="product_catalog_name"/>
//		 <field column="price" name="product_price"/>
//		 <field column="description" name="product_description"/>
//		 <field column="picture" name="product_picture"/>
    @Test
    public void addCli() throws IOException, SolrServerException {
        //创建solr客户端对象
        HttpSolrClient httpSolrClient = getSolrClient();
        //创建一个文档对象
        SolrInputDocument solrInputFields = new SolrInputDocument();
        solrInputFields.addField("id", "c001");
        solrInputFields.addField("product_name", "solr没有");
        solrInputFields.addField("product_catalog_name", "IT技术");
        solrInputFields.addField("product_price", "1234");
        solrInputFields.addField("product_description", "我是黑马程序员");
        solrInputFields.addField("product_picture", "3124.jpg");
        httpSolrClient.add(solrInputFields);
        httpSolrClient.commit();
        httpSolrClient.close();
    }

    //查询ID删除
    @Test
    public void delDoc() throws Exception {
        HttpSolrClient solrClient = getSolrClient();
//        删除多个ID
//        ArrayList<String> strings = new ArrayList<String>();
//        strings.add("99");
//        strings.add("98");
//        strings.add("97");
//        solrClient.deleteById(strings);
        solrClient.deleteById("c001");
        solrClient.commit();
        solrClient.close();
    }

    //查询对象删除
    @Test
    public void queryDel() throws Exception {
        HttpSolrClient solrClient = getSolrClient();
        solrClient.deleteByQuery("product_name:青蛙");
        solrClient.commit();
    }

    //根据ID查询
    @Test
    public void searCh() throws Exception {
        HttpSolrClient solrClient = getSolrClient();
        SolrDocument c001 = solrClient.getById("c001");
        Collection<String> fieldNames = c001.getFieldNames();
        for (String fieldName : fieldNames) {
            System.out.print(fieldName + "=");
            Collection<Object> fieldValues = c001.getFieldValues(fieldName);
            for (Object fieldValue : fieldValues) {
                System.out.println(fieldValue);
            }
        }
    }

    @Test
    public void querySearch() throws IOException, SolrServerException {
        //获取solr客户端对象
        HttpSolrClient solrClient = getSolrClient();
        //设置查询条件
        SolrQuery solrQuery = new SolrQuery();
        //设置高亮
        solrQuery.setHighlight(true);
        solrQuery.addHighlightField("product_name");
        solrQuery.setHighlightSimplePre("<span>");
        solrQuery.setHighlightSimplePost("</span>");
        //如果有多个查询类型就继续在后面set
        solrQuery.set("q", "product_name:浪漫");
        solrQuery.set("fq", "product_price:[70 TO 90]");
        //设置升序降序
        solrQuery.setSort("product_price", SolrQuery.ORDER.asc);
        solrQuery.setSort("id", SolrQuery.ORDER.asc);
        //设置分页
        solrQuery.setStart(2);
        solrQuery.setRows(2);
        //设置高亮
        solrQuery.setHighlightSimplePre("<span>");
        solrQuery.setHighlightSimplePost("</span>");
        solrQuery.setHighlight(true);
        //这种的是 or查询
        //solrQuery.set("q","product_name:浪漫 OR　product_name:卡通");
        //solrQuery.set("fq","product_keywords:韩国");
        //执行查询条件
        QueryResponse query = solrClient.query(solrQuery);
        //返回查询结果
        SolrDocumentList results = query.getResults();
        //获得查询总数
        System.out.println(results.getNumFound());
        for (SolrDocument result : results) {
            String id = (String) result.getFieldValue("id");
            String product_name = (String) result.getFieldValue("product_name");
            String product_catalog_name = (String) result.getFieldValue("product_catalog_name");
            Float product_price = (Float) result.getFieldValue("product_price");
            String product_description = (String) result.getFieldValue("product_description");
            System.out.print("ID=" + id);
            System.out.print(",product_name=" + product_name);
            System.out.print(",product_catalog_name=" + product_catalog_name);
            System.out.print(",product_price=" + product_price);
            System.out.println(",product_description=" + product_description);
            System.out.println("----高亮----");
            //获得高亮
            Map<String, Map<String, List<String>>> highlighting = query.getHighlighting();
            if (null != highlighting) {
                Map<String, List<String>> stringListMap = highlighting.get(id);
                List<String> product_name1 = stringListMap.get("product_name");
                if (product_name1 != null) {
                    for (String s : product_name1) {
                        System.out.println(s);
                    }
                }
            }
        }
    }

    @Test
    public void searchDoc() throws IOException, SolrServerException {
        HttpSolrClient solrClient = getSolrClient();
        SolrQuery solrQuery = new SolrQuery();
        solrQuery.setHighlight(true);
        solrQuery.addHighlightField("product_name");
        solrQuery.addHighlightField("product_catalog_name");
        solrQuery.setHighlightSimplePre("<span>");
        solrQuery.setHighlightSimplePost("</span>");
        solrQuery.set("q", "product_name:时尚 AND product_catalog_name:时尚卫浴");
        QueryResponse query = solrClient.query(solrQuery);
        SolrDocumentList results = query.getResults();
        System.out.println(results.getNumFound());

        for (SolrDocument result : results) {
            Map<String, Map<String, List<String>>> highlighting = query.getHighlighting();
            Map<String, List<String>> id = highlighting.get(result.getFieldValue("id"));
            List<String> product_name = id.get("product_name");
            for (String s : product_name) {
                System.out.print(s + "=");
            }
            List<String> product_catalog_name = id.get("product_catalog_name");
            for (String s : product_catalog_name) {
                System.out.println(s);
            }
        }
    }
    @Test
    public void T() throws IOException, SolrServerException {
        HttpSolrClient solrClient = getSolrClient();
        SolrQuery solrQuery = new SolrQuery();
        solrQuery.set("q","product_name:时尚")
                 .set("fq","product_catalog_name:时尚卫浴");

        solrQuery.setHighlight(true)
                .setHighlightSimplePre("<span>")
                .setHighlightSimplePre("</span>")
                .addHighlightField("product_name")
                .addHighlightField("product_catalog_name");

        QueryResponse query = solrClient.query(solrQuery);
        SolrDocumentList results = query.getResults();
        System.out.println(results.getNumFound()+"总数量");
        for (SolrDocument result : results) {
            Object id = result.getFieldValue("id");
            Object product_catalog_name = result.getFieldValue("product_catalog_name");
            System.out.println(id);
            System.out.println(product_catalog_name);
            Map<String, Map<String, List<String>>> highlighting = query.getHighlighting();
            Map<String, List<String>> stringListMap = highlighting.get(id);
            List<String> product_name = stringListMap.get("product_name");
            if (null != product_name){
                for (String s : product_name) {
                    System.out.println(s);
                }
            }
            List<String> product_catalog_name1 = stringListMap.get("product_catalog_name");
            if (null !=product_catalog_name1){
                for (String s : product_catalog_name1) {
                    System.out.println(s);
                }
            }
        }
    }
}
